package com.zzld.modules.autoinn.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.abel533.entity.Example;
import com.github.pagehelper.PageInfo;
import com.zzld.core.utils.MessageUtils;
import com.zzld.modules.autoinn.entity.UserRegister;
import com.zzld.modules.autoinn.service.IUserRegisterService;
import com.zzld.modules.common.dto.CommonJSON;

@RequestMapping("/autoinn/userRegister")
@Controller
public class RegisterController {

	private String listurl = "/modules/autoinn/userregister/userregisterlist";
	private String detail = "/modules/autoinn/userregister/userregisterdetail";

	@Autowired
	private IUserRegisterService registerService;

	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return listurl;
	}

	@RequestMapping(value = "detail/{id}")
	public String detail(Model model, HttpServletRequest request,
			@PathVariable("id") String id) {
		UserRegister register = null;
		try {
			if (StringUtils.isNotBlank(id)) {
				register = registerService.selectByPrimaryKey(id);	
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("register", register);
		return detail;
	}
	
	@RequestMapping("/update/pro/{id}")
	@ResponseBody
	public CommonJSON update(HttpServletRequest request,@PathVariable("id") String id){
		CommonJSON json = new CommonJSON();
	
		try{
			if(StringUtils.isNotBlank(id)){
				UserRegister usergister =new UserRegister();
				usergister.setId(id);
				usergister.setState(1);
				if(registerService.updateByPrimaryKeySelective(usergister)>0){
					json.setMessage(MessageUtils.EDIT_SUCCESS);
					json.setSuccess(true);
				}
			}
			
		}catch(Exception e){
	        json.setMessage(MessageUtils.EDIT_WARING);
	        json.setSuccess(false);
		}
		
		
		return json;
	}

	@RequestMapping("list")
	@ResponseBody
	public CommonJSON list(HttpServletRequest request) {

		CommonJSON json = new CommonJSON();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("nickname", request.getParameter("nickname"));
		map.put("phone", request.getParameter("phone"));
		int pageNum = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("rows"));

		try {
			PageInfo pageinfo = registerService.list(map, pageNum, pageSize);
			json.setRows(pageinfo.getList());
			json.setTotal(pageinfo.getTotal());
			json.setSuccess(true);
		} catch (Exception e) {
			json.setSuccess(false);
		}
		return json;
	}

}
